/**
 * 广告管理初始化
 */
var WzAdv = {
    id: "WzAdvTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
WzAdv.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '广告编号', field: 'advId', visible: true, align: 'center', valign: 'middle'},
        {title: '广告标题', field: 'title', visible: true, align: 'center', valign: 'middle'},
        {
            title: '封面图片', field: 'coverUrl', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value != undefined && value != '') {
                    value = '<img src="' + value + '" width="40" height="40" style="margin:-8px auto;"/>';
                }
                return value;
            }
        },
        {title: '发布类型', field: 'publishingType', visible: true, align: 'center', valign: 'middle'},
        {title: '广告类型', field: 'type', visible: true, align: 'center', valign: 'middle'},
        {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle'},
        {title: '赏金总额', field: 'totalReward', visible: true, align: 'center', valign: 'middle'},
        {title: '剩余赏金', field: 'surplusReward', visible: true, align: 'center', valign: 'middle'},
        {title: '赏金平均值', field: 'rewardAverage', visible: true, align: 'center', valign: 'middle'},
         {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
        // {title: '最近修改时间', field: 'updateTime', visible: true, align: 'center', valign: 'middle'},
    ];
};

/**
 * 检查是否选中
 */
WzAdv.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        WzAdv.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加广告
 */
WzAdv.openAddWzAdv = function () {
    var index = layer.open({
        type: 2,
        title: '添加广告',
        area: ['1200px', '750px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/wzAdv/wzAdv_add'
    });
    this.layerIndex = index;
};

/**
 * 点击添加选项卡广告
 */
WzAdv.openAddWzAdvTab = function () {
    var index = layer.open({
        type: 2,
        title: '添加广告',
        area: ['1200px', '750px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/wzAdv/wzAdv_add_tab'
    });
    this.layerIndex = index;
};

/**
 * 打开查看广告详情
 */
WzAdv.openWzAdvDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '广告详情',
            area: ['1200px', '750px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/wzAdv/wzAdv_update/' + WzAdv.seItem.advId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除广告
 */
WzAdv.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/wzAdv/delete", function (data) {
            if(data.code == 100){
                Feng.error(data.message);
            }else if(data.code == 200){
                Feng.success("删除成功!");
                WzAdv.table.refresh();
            }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("wzAdvId", WzAdv.seItem.advId);
            ajax.start();
        };
        Feng.confirm("是否刪除该广告?", operation);
    }
};

/**
 * 广告提交审核
 */
WzAdv.checkAdv = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/wzAdv/checkAdv", function (data) {
            if(data.code == 100){
                Feng.error(data.message);
            }else if(data.code == 200){
                Feng.success("提交审核成功!");
                WzAdv.table.refresh();
            }
            }, function (data) {
                Feng.error("提交审核失败!" + data.responseJSON.message + "!");
            });
            ajax.set("wzAdvId", WzAdv.seItem.advId);
            ajax.start();
        };
        Feng.confirm("是否提交审核?", operation);
    }
};


/**
 * 广告发布
 */
WzAdv.release = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/wzAdv/release", function (data) {
            if(data.code == 100){
                Feng.error(data.message);
            }else if(data.code == 200){
                Feng.success("发布广告成功!");
                WzAdv.table.refresh();
            }
            }, function (data) {
                Feng.error("发布广告失败!" + data.responseJSON.message + "!");
            });
            ajax.set("wzAdvId", WzAdv.seItem.advId);
            ajax.start();
        };
        Feng.confirm("是否发布广告?", operation);
    }
};

/**
 * 查询广告列表
 */
WzAdv.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    WzAdv.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = WzAdv.initColumn();
    var table = new BSTable(WzAdv.id, "/wzAdv/list", defaultColunms);
    table.setPaginationType("server");
    WzAdv.table = table.init();
});
